PROJECTENGRAM

JAVASCRIPT DOM

JavaScript DOM

Tags: #◇Web Development #◇CCAPDEV #◇JavaScript DOM #◇Code Snippets #◇M3


◇Overview

Extracted notes for JavaScript DOM covering both theoretical and practical elements.

◇Key Definitions

Document Object Model Allows programs and scripts to dynamically access and update the content, structure, and style of a document. Can change all HTML elements Can change all HTML attributes Can change all CSS styles The HTML DOM API essentially provides write access to the HTML document through JavaScript
Document Object Model HTML DOM is a standard object model and programming interface for HTML. Figure 1. An HTML document in DOM format
DOM Methods Finding HTML elements: document.getElementById(id) document.getElementsByTagName(name) document.getElementsByClassName(class)
After getting references to the elements... You can change the following: Content Attributes Styles
DOM Nodes https://developer.mozilla.org/en-US/docs/Web/API/Node

◇Code Implementation

『』
TEXT
Get Element By ID
The easiest way to find an HTML element in the DOM is by using the element id
<p id=“txt1”> Text 1 </p>
<p id=“txt2”> Text 2 </p>
document.getElementById(“txt1”).innerHTML=“Hi!”;
『』
TEXT
Get Elements By TAG
This will return an array of objects of a specific <tag>
<p id=“intro”> Hello world! </p>
<p> Hello universe! </p>
<p class=“comment”> New comment </p>
document.getElementsByTagname(“p”);
『』
TEXT
Get Elements By CLASS
This will return an array of objects of a specific class name.
<p class=“txt”> Text 1 </p>
<p> Hello universe! </p>
<p class=“txt”> Text 2 </p>
var txtElements = document.getElementsByClassName(“txt”);
『』
TEXT
Changing Content
HTML
<p id=“p1”> Hello World </p>
JS
document.getElementById(“p1”).innerHTML = “New Text!”;
https://developer.mozilla.org/en-US/docs/Web/API/Element
『』
TEXT
Changing Content
HTML
<p id=“p1”> Hello World </p>
JS
var x = document.getElementById(“p1”);
x.innerHTML = “New Text!”;
https://developer.mozilla.org/en-US/docs/Web/API/Element

◇Syntax Breakdown

Ensure correct syntax for JavaScript DOM Pay attention to specific functions and declarations.

◇Complexity Analysis

Time Complexity: O(1) for typical operations unless looping. Space Complexity: O(1) generally.

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

What does the HTML Document Object Model (DOM) primarily allow scripts to do?

Q2

Which DOM method is used to return an array of objects based on a specific HTML tag, such as <p>?

Q3

How would you change the textual content of an HTML element with the id "p1" using JavaScript?